package com.car.job;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.car.action.admin.CardController;
import com.car.common.DefaultConfig;
import com.car.common.model.Card;
import com.car.common.model.CardDiscuss;
import com.car.common.model.CardImg;
import com.car.common.model.MessageUnreadNum;
import com.car.common.model.NavyUsers;
import com.car.common.model.TempWrap;
import com.car.common.model.UserFans;
import com.car.common.model.UserHellos;
import com.car.common.model.UserSees;
import com.car.common.model.Users;
import com.car.tx.UserWrap;
import com.car.tx.UserWrap2;
import com.dvzju.lotto.k3.Rand;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.utils.DateUtil;
import com.utils.HttpRequestUtil;
import com.utils.JwtToken;
import com.utils.RongCloudUtil;
import com.utils.StringUtil;
import com.utils.SystemClock;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by chengjun on 2019/11/15.
 */
public class QuartzCard implements Job {
	
	private static Logger logger = Logger.getLogger(QuartzCard.class);
	
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    	Calendar calendar = Calendar.getInstance();
    	int curHour24 = calendar.get(Calendar.HOUR_OF_DAY);
//    	System.out.println(DateUtil.commonDateTimeFormat.format(calendar.getTime()) );
    	if (curHour24 < 9) {
    		// 给发帖女号加关注
    		/*
			List<Card> card_list = Card.dao.find("select card.user_id,u.area_id from card LEFT join users as u on card.user_id = u.id where u.sex = 2 GROUP BY card.user_id ORDER BY RAND() limit 20 ");
			for(Card card :card_list) {
				String sql = "select id  from users where sex = 1 and is_true > 1 and area_id = "+ card.get("area_id")+" ORDER BY RAND() LIMIT 3 ";
    		    List<Users> users = Users.dao.find(sql);
    		    for(Users u : users) {
    		    	Long user_id = u.getId(); 
    		    	int target_user_id = card.getUserId();
    		    	UserSees one1 = new UserSees();
    	 			one1.setUserId(user_id.intValue());
    	 			one1.setTargetUserId(target_user_id);
    	 			one1.setCreateTime(SystemClock.now()/1000L - 1 * 60);
    	 			one1.setUpdateTime(SystemClock.now()/1000L);
    	 			if (one1.save()) {
    	 				new CardController().giveABPoint(user_id.intValue());
    	 				MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_SEE, target_user_id);
    	 			}
    		    	//判断是否关注
    				UserFans one = UserFans.dao.findFirst("select id , user_id from user_fans where user_id = " + user_id+ " and target_user_id = " + target_user_id +" and delete_time is null ");
    				if (one == null) {
    					one = new UserFans();
    					one.setUserId(user_id.intValue());
    					one.setTargetUserId(target_user_id);
    					one.setIsAuth(1);
    					one.setStatus(1);
    					one.setCreateTime(SystemClock.now()/1000L);
    					one.setUpdateTime(SystemClock.now()/1000L);
    					if (one.save()) {
    						new CardController().giveABPoint(user_id.intValue());
    						MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_FAN, target_user_id);
    					}	
    				} 
    		    }
    		   
			}
			 */
    			
    		// 1点 ~ 9点 什么都不发帖
    		int rand = new Random().nextInt(2);
    		if (rand == 1) {
    			Db.update("update card set create_time = " + SystemClock.FUTURE_TIME +" where update_time = " + SystemClock.FUTURE_TIME +" and create_time < " + (SystemClock.now()/1000L - 30*24*60*60L));
    			Card card = Card.dao.findFirst("select id,user_id from card where create_time = " + SystemClock.FUTURE_TIME +" order by rand() limit 1 ");
        		if (card != null) {
        			Users users = Users.dao.findById(card.getUserId());
    				Users c_users = Users.dao.findFirst("SELECT t1.id FROM `users` AS t1 left join users_info as t2 on t1.id = t2.user_id  WHERE t2.lat is not null and t1.sex = "+users.getSex()+" AND t1.is_true = 3  ORDER BY RAND() LIMIT 1;");
    				Long user_id = c_users.getId();
    				Db.update("update users set login_status = 0,logout_time = "+ (SystemClock.now()/1000L + 5*6*60L ) + "  where id = " + user_id);
    				Db.update("update card set user_id = " + user_id + " where id = " +  card.getId());
    				Db.update("update card_discuss set create_time = " + (SystemClock.now()/1000L + 10*6*60L )  + " where card_id = " +  card.getId());
        				
        			List<CardDiscuss> discussList= CardDiscuss.dao.find("select id from discuss where card_id = " +  card.getId() );
        			for (CardDiscuss cardDiscuss : discussList) {
        				Users d_users = Users.dao.findFirst("SELECT t1.id FROM `users` AS t1 left join users_info as t2 on t1.id = t2.user_id  WHERE t2.lat is not null and t1.sex = "+users.getSex()+" AND t1.is_true = 3  ORDER BY RAND() LIMIT 1;");
        				Db.update("update card_discuss set create_time = " + (SystemClock.now()/1000L + new Random().nextInt(300) ) + ",user_id = "+ d_users.getId()+ " where id = " +  cardDiscuss.getId());
					}
        			
        		}
        		
        		Card card_aicishi = Card.dao.findFirst("select id,user_id,content from card where update_time = " + SystemClock.FUTURE_TIME +" order by rand() limit 1 ");
        		List<CardImg> imgs = CardImg.dao.find("select * from card_img where card_id = " + card_aicishi.getId());
        		String images = "";
        		for(CardImg img :imgs) {
        			images += img.getImageUrl() + ",";
        		}
        		if (images.length() > 0) {
        			images = images.substring(0, images.length() -1);
        		}
        		try {
        			String params = "images="+images+"&topic_id="+card_aicishi.getTopicId()+"&content="+card_aicishi.getContent()+"&token="+JwtToken.createToken(200000L+ card_aicishi.getUserId());
        			System.out.println("api.aicishi.cn:"+HttpRequestUtil.sendPost("http://api.aicishi.cn/card/into_card",params));
        		} catch (Exception e) {
        			e.printStackTrace();
        		}
    		}
    		
    	} else {
    		// 随机 B ,C 中会员 
    		if (new Random().nextInt(4) == 2) {
    			Users luck = Users.dao.findFirst("select id,vip_end_time,vip_badpe from users  as u where  u.vip_badpe = 0 and ( u.is_true = 2 or  u.is_true = 3 ) ORDER BY RAND() LIMIT 1 ");
    			Db.update("update users set vip_end_time = " + (SystemClock.now()/1000L + 7*24*60*60L)+",vip_badpe = 1 where id = "+ luck.getId());
    		}
    		
	    		Card card = Card.dao.findFirst("select id,user_id from card where create_time = " + SystemClock.FUTURE_TIME +" order by rand() limit 1 ");
	    		if (card != null) {
	    			Users users = Users.dao.findById(card.getUserId());
	    				Users c_users = Users.dao.findFirst("SELECT t1.id FROM `users` AS t1 left join users_info as t2 on t1.id = t2.user_id  WHERE t2.lat is not null and t1.sex = "+users.getSex()+" AND t1.is_true = 3  ORDER BY RAND() LIMIT 1;");
	    				Long user_id = c_users.getId();
	    				Db.update("update users set login_status = 0,logout_time = "+ (SystemClock.now()/1000L + 3*6*60L ) + "  where id = " + user_id);
	    				Db.update("update card set user_id = " + user_id + " where id = " +  card.getId());
	    				
	    				Db.update("update card_discuss set create_time = " + (SystemClock.now()/1000L + 10*6*60L )  + " where card_id = " +  card.getId());
	    				
	    			long create_time = SystemClock.now()/1000L + new Random().nextInt(300);
	    			Db.update("update card set create_time = " + create_time + ",t_create_time = " + create_time + " where id = " +  card.getId());
	    			List<CardDiscuss> discussList= CardDiscuss.dao.find("select id from discuss where card_id = " +  card.getId() );
        			for (CardDiscuss cardDiscuss : discussList) {
        				Users d_users = Users.dao.findFirst("SELECT t1.id FROM `users` AS t1 left join users_info as t2 on t1.id = t2.user_id  WHERE t2.lat is not null and t1.sex = "+users.getSex()+" AND t1.is_true = 3  ORDER BY RAND() LIMIT 1;");
        				Db.update("update card_discuss set create_time = " + (SystemClock.now()/1000L + new Random().nextInt(300) ) + ",user_id = "+ d_users.getId()+ " where id = " +  cardDiscuss.getId());
					}
	    		} 
    		
    		if (new Random().nextInt(3) == 2) {
    			Card card_aicishi = Card.dao.findFirst("select id,user_id,content from card where update_time = " + SystemClock.FUTURE_TIME +" order by rand() limit 1 ");
        		List<CardImg> imgs = CardImg.dao.find("select * from card_img where card_id = " + card_aicishi.getId());
        		String images = "";
        		for(CardImg img :imgs) {
        			images += img.getImageUrl() + ",";
        		}
        		if (images.length() > 0) {
        			images = images.substring(0, images.length() -1);
        		}
        		try {
        			String params = "images="+images+"&topic_id="+card_aicishi.getTopicId()+"&content="+card_aicishi.getContent()+"&token="+JwtToken.createToken(200000L+ card_aicishi.getUserId());
        			System.out.println("api.aicishi.cn:"+HttpRequestUtil.sendPost("http://api.aicishi.cn/card/into_card",params));
        		} catch (Exception e) {
        			e.printStackTrace();
        		}
    		}
    	}
    	
    	// 随机评论
		List<CardDiscuss> discuss_list = CardDiscuss.dao.find("select discuss.id,discuss.user_id,discuss.card_id from card_discuss as discuss left join card  on discuss.card_id = card.id where discuss.card_id > 0 and card.create_time <  " + SystemClock.now()/1000L + " and discuss.create_time = " + SystemClock.FUTURE_TIME +" order by rand() limit 20 ");
		for(CardDiscuss discuss:discuss_list) {
			Db.update("update users set login_status = 0,logout_time = "+ (SystemClock.now()/1000L + 5 * 6 * 60L ) + "  where id = " + discuss.getUserId());
			
			Db.update("update card set discuss = discuss + 1,discuss_cnt = discuss_cnt + 1,views = views + 50,t_views = t_views + 50 where id = " + discuss.getCardId());
			Db.update("update card_discuss set create_time = " + (SystemClock.now()/1000L + new Random().nextInt(400) ) + " where id = " +  discuss.getId());
//			System.out.println("update card set create_time = " + (SystemClock.now()/1000L + new Random().nextInt(300) ) + " where id = " +  card.getId());
		}
    	
		// 邀请好友 热度 不处理的回收
		List<UserHellos> hellos = UserHellos.dao.find("select id,user_id,target_user_id,status,hot_status,hot,create_time from user_hellos where hot > 0 and hot_status = 0 and status = 2 and create_time < " + (SystemClock.now()/1000L - 7*24*60*60L) );
		for(UserHellos one : hellos) {
			// 热度退还
			Db.update("update users set hot_val = hot_val + " + one.getInt("hot") +" where id = " + one.getUserId());
			Db.update("update user_hellos set hot_status = 1 where id = " + one.getId());
			RongCloudUtil.sendTxt("141", String.valueOf(one.getUserId()), "系统退还热度:" + one.getInt("hot"));
		}
		
		// 昵称
		String[] step1 = {"黑白","独行","微笑","童心","幻想","英俊","浮夸","低调","善良","落叶","纯情","流浪","虔诚","暴走","无情","寂寞","无聊","超神","帅气","黑白","黑白","独行","微笑","童心","幻想","英俊","浮夸","低调","善良","落叶","纯情","流浪","虔诚","暴走","无情","寂寞","无聊","超神","帅气","青青","不帅","懒懒","蓝蓝","青涩","害羞","晴空","美好","寒冷","火热","可爱","潇洒","无情","怀旧","念旧","慈悲","苦涩","青春","清纯","希望","疼痛","流浪","悲伤","深情","忧伤","欢乐","欢快","夏天","春天","秋天","冬天","夏日","春日","秋日","冬日","银白","深蓝","纯白","纯黑","淡紫","浓墨","灰白","仿徨","水墨","梦里","大气","大方","热情","热心","孤独","寂寞","孤单","沧桑","午夜","深夜","清晨","黎明","陌生","熟悉","当年","曾经","闪烁","繁华","甜蜜","黑夜","半夜","七夕","幸福","逆光","迎风","坚强","永恒","七月","六月","三月","九月","八月","十月","五月","贰月","壹月","绚丽"};
		
		String[] step2 = {"传说","自由","竹子","旋律","旋涡","豆浆","潇洒","守护","牡丹","皇族","墨墨","演绎","熊猫","佐助","王子","麦兜","包子","浮云","精英","回忆","传说","自由","竹子","旋律","旋涡","豆浆","潇洒","守护","牡丹","皇族","墨墨","演绎","熊猫","佐助","王子","麦兜","包子","浮云","精英","回忆","孤山","山谷","记忆","丸子","情话","山楂","眼泪","表情","思念","孤寂","大海","小雨","飘雪","花香","梅花","樱花","寒梅","梨花","往事","东风","秋月","念想","春花","寒冰","空城","美梦","美酒","背影","容颜","角色","主角","主演","江山","余生","雨声","红尘","情歌","清风","山峰","湖水","河流","溪水","雪山","江水","大地","烟雨","晚霞","空巷","小巷","街道","石桥","归人","星星","月亮","宇宙","星辰","故事","今生","爱情","风景","街口","信纸","触摸","时光","年华","青春","微光","吟唱","回眸","拥抱","痕迹","泪水","过往","过去","结局","转身","红茶","红酒","清茶","浮生","云朵"};		

		List<Users> usersList = Users.dao.find("select id from users where nick_name ='火星人' or nick_name is null ");
		for(Users one :usersList) {
			String newNickName = step1[new Random().nextInt(step1.length)] +"的"+step2[new Random().nextInt(step2.length)];
			Db.update("update users set nick_name = '" + newNickName+"' where id = " + one.getId());
		}
		// 头像
		String[] heads = {"51du_system_head01.png","51du_system_head02.png","51du_system_head03.png","51du_system_head04.png","51du_system_head05.png","51du_system_head06.png"};
		List<Users> usersHeadList = Users.dao.find("select id from users where user_header is null or length(user_header) = 0 and is_true = 1 and sex = 1 ");
		for(Users one :usersHeadList) {
			String user_header = heads[new Random().nextInt(heads.length)] ;
			Db.update("update users set user_header = '" + user_header+"' where id = " + one.getId());
		}
		// 没设置的 生日 1998-08-08
		Db.update("update users set birthday = 902505600 where is_true = 1 and birthday is null");
		// 没地址 海外去
		Db.update("update users set area_id = 190311   where is_true = 1 and area_id is null ");
		
		Db.update("update users set vip_badpe = 0 where vip_badpe > 0 and vip_end_time < "+ SystemClock.now()/1000L +" and is_true = 1 ");
		
		Db.update("update card_discuss set buser_id = user_id where buser_id = 0 ");
		/*
		String andSql = "select id from users as u where is_true > 1 and login_status > 0  ";
		List<Users> users = Users.dao.find(andSql + " order by rand() limit " + 200);
        for(Users one : users) {
        	Db.update("update usRers set login_status = 0,logout_time = "+ (SystemClock.now()/1000L + 5 * 6 * 60L + new Random().nextInt(500)* 3 *6L ) +"  where id = " + one.getId());
        }
        */
		
    }
    
    
    public static void main(String[] args) {
    	for(int i = 0 ; i < 100;i++) {
    		
    		System.out.println(new Random().nextInt(2));
    	}
	}
}
